package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.base.mapper.mysql.JyGoodsMapper;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassemblydetail;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetail;
import com.jy.adscm.operation.mscyb.bean.JqzzMachinepartsdetail;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMachinepartsdetailMapper;
import com.jy.adscm.operation.mscyb.service.JqzzMachinepartsdetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-04
 */
@Service
public class JqzzMachinepartsdetailServiceImpl extends ServiceImpl<JqzzMachinepartsdetailMapper, JqzzMachinepartsdetail> implements JqzzMachinepartsdetailService {
    @Autowired
    JqylMaterialMapper jqylMaterialMapper;
    @Autowired
    JyGoodsMapper jyGoodsMapper;
    @Autowired
    JqylMaterialdetailMapper jqylMaterialdetailMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public boolean insertJqzzMachinepartsdetail(JqzzMachinepartsdetail jqzzMachinepartsdetail, Token token) throws AdscmSystemException {

        //根据查询的原料号
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinepartsdetail.getPartsname(),null)){
            JqylMaterial jqylMaterial = jqylMaterialMapper.selectById(jqzzMachinepartsdetail.getPartsname());
            //进行校验
            if (!AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial,null)){
                throw  new AdscmSystemException("没找到任务书信息");
            }
            if (!"1".equals(jqylMaterial.getIstaskbegin())){
                throw  new AdscmSystemException("物料未入库，不能添加出库信息");
            }
            if (jqylMaterial.getRemainingnums()==0){
                throw  new AdscmSystemException("物料已经用完");
            }
            if (jqylMaterial.getRemainingnums()<jqzzMachinepartsdetail.getNums()){
                throw new AdscmSystemException("使用量超过了剩余量");
            }
            //减去任务书中的剩余数量
            DecimalFormat df=new DecimalFormat("#0.0000");
            String format = df.format(jqylMaterial.getRemainingnums()-jqzzMachinepartsdetail.getNums());
            jqylMaterial.setRemainingnums( Float.valueOf(format) );
            jqylMaterialMapper.updateById(jqylMaterial);
            //根据任务书中的物料名称获取到物料的编码
            JyGoods jyGoods = jyGoodsMapper.selectById(jqylMaterial.getResourcename());
            //物料编码
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyGoods.getProductcode(),null)){
                String productcode = jyGoods.getProductcode();
                if (jqzzMachinepartsdetail.getNums()>1){
                    throw  new AdscmSystemException("添加数量大于1，不能生成对应条码");
                }
                String yearMonth = new SimpleDateFormat("yyMM").format(new Date());
                Wrapper<JqylMaterialdetail> wrapper=new EntityWrapper<>();
                wrapper.like("CODE", productcode + yearMonth, SqlLike.RIGHT);
                wrapper.orderBy("CODE",false);
                List<JqylMaterialdetail> jqylMaterialdetails = jqylMaterialdetailMapper.selectList(wrapper);
                String code="";
                if (jqylMaterialdetails.size()>0){
                    JqylMaterialdetail oldjqylMaterialdetail = jqylMaterialdetails.get(0);
                    String oldCode = oldjqylMaterialdetail.getCode();
                    Integer temp = Integer.valueOf(oldCode.substring(oldCode.length()-2, oldCode.length())) ;
                    temp=temp+1;
                    String nums = new DecimalFormat("00").format(temp);
                    code=productcode+yearMonth+nums;
                }else{
                    code=productcode+yearMonth+"00";
                }
                code.trim();
                jqzzMachinepartsdetail.setPartscode(code);
            }
            //部件使用信息
            jqzzMachinepartsdetail.setCreatetime(new Date());
            jqzzMachinepartsdetail.setPartsbatch(jqylMaterial.getBatchnums());
            jqzzMachinepartsdetail.setPartsrealname(jqylMaterial.getResourcename());
            jqzzMachinepartsdetail.setPartstype(jqylMaterial.getTypenums());
            jqzzMachinepartsdetail.setUnit(jqylMaterial.getUnit());
            jqzzMachinepartsdetail.setUserperson(token.getUser().getId().toString());
            //添加原料库中的出库记录
            JqylMaterialdetail jqylMaterialdetail=new JqylMaterialdetail();
            jqylMaterialdetail.setName(jqylMaterial.getResourcename());
            jqylMaterialdetail.setTypenums(jqylMaterial.getTypenums());
            jqylMaterialdetail.setUnit(jqylMaterial.getUnit());
            jqylMaterialdetail.setBatchnums(jqylMaterial.getBatchnums());
            jqylMaterialdetail.setCode(jqzzMachinepartsdetail.getPartscode());
            jqylMaterialdetail.setOutstorageaddtime(new Date());
            jqylMaterialdetail.setUseperson(token.getUser().getId().toString());
            jqylMaterialdetail.setIsrepair(0);
            jqylMaterialdetail.setUsenums(jqzzMachinepartsdetail.getNums());
            jqylMaterialdetail.setMaterialstate(jqzzMachinepartsdetail.getMaterialstate());
            jqylMaterialdetail.setMid(jqzzMachinepartsdetail.getTid());
            jqylMaterialdetail.setOutstoragetime(jqzzMachinepartsdetail.getUsetime());
            jqylMaterialdetail.setTid(jqylMaterial.getTid().toString());
            jqylMaterialdetailMapper.insert(jqylMaterialdetail);
        }
        boolean insert = super.insert(jqzzMachinepartsdetail);
        return insert;
    }

    @Override
    public Boolean updateJqzzMachinepartsdetail(JqzzMachinepartsdetail jqzzMachinepartsdetail) {
        boolean b = super.updateById(jqzzMachinepartsdetail);
        return b;
    }

    @Override
    public List<JqzzMachinepartsdetail> selectByTid(String tid) {
        Wrapper<JqzzMachinepartsdetail> wrapper =new EntityWrapper<>();
        wrapper.eq("TID",tid);
        List<JqzzMachinepartsdetail> JqzzMachinepartsdetails = super.selectList(wrapper);
        for (JqzzMachinepartsdetail jqzzMachinepartsdetail : JqzzMachinepartsdetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinepartsdetail.getUserperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzMachinepartsdetail.getUserperson());
                jqzzMachinepartsdetail.setUserpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinepartsdetail.getPartsrealname(),null)){
                JyGoods jyGoods = jyGoodsMapper.selectById(jqzzMachinepartsdetail.getPartsrealname());
                jqzzMachinepartsdetail.setPartsrealnameName(jyGoods.getName());
            }
        }
        return JqzzMachinepartsdetails;
    }

    @Override
    public List<JqylMaterial> queryMaterial() {
        Wrapper<JqylMaterial> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("TID","RESOURCENAME");
        wrapper.eq("ISTASKBEGIN",1);
        wrapper.eq("ISTASKEND",0);
        List<JqylMaterial> jqylMaterials = jqylMaterialMapper.selectList(wrapper);
        for (JqylMaterial jqylMaterial : jqylMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getResourcename(),null)){
                JyGoods jyGoods = jyGoodsMapper.selectById(jqylMaterial.getResourcename());
                jqylMaterial.setResourcenameName(jyGoods.getName());
            }
        }
        return jqylMaterials;
    }
}
